﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DHQGHN.PM0.Common.Attributes;
using DHQGHN.PM0.Controllers.Admin;

namespace DHQGHN.PM0.Models.Admin
{
    public class MenuModel
    {
        [Range(1, Int64.MaxValue, ErrorMessage = "Bạn cần chọn chức năng")]
        [LocalizationDisplay("Chức năng")]
        public int MenuId { get; set; }

        //[Remote("CheckExistName", "Menu", ErrorMessage = "MenuNameExist", HttpMethod = "POST", AdditionalFields = "MenuId")]
        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "Error_HaventPutMenuNameYet", ErrorMessageResourceType = typeof(Resources.Localization))]
        [StringLength(200, ErrorMessageResourceName = "Error_MenuNameLength", ErrorMessageResourceType = typeof(Resources.Localization))]
        [LocalizationDisplay("MenuName")]
        public string MenuName { get; set; }

        //[Remote("CheckExistCode", "Menu", ErrorMessage = "MenuCodeExist", HttpMethod = "POST", AdditionalFields = "MenuId")]
        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "Error_HaventPutMenuCodeYet", ErrorMessageResourceType = typeof(Resources.Localization))]
        [StringLength(50, ErrorMessageResourceName = "Error_MenuCodeLength", ErrorMessageResourceType = typeof(Resources.Localization))]
        [LocalizationDisplay("MenuCode")]
        public string MenuCode { get; set; }

        [StringLength(1000, ErrorMessageResourceName = "Error_MenuDescriptionLength", ErrorMessageResourceType = typeof(Resources.Localization))]
        [LocalizationDisplay("Description")]
        public string MenuDescription { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "Error_HaventPutMenuUrlYet", ErrorMessageResourceType = typeof(Resources.Localization))]
        [StringLength(500, ErrorMessageResourceName = "Error_MenuUrlLength", ErrorMessageResourceType = typeof(Resources.Localization))]
        [LocalizationDisplay("MenuUrl")]
        public string MenuUrl { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "Error_HaventPutAppNameYet", ErrorMessageResourceType = typeof(Resources.Localization))]
        [LocalizationDisplay("AppName")]
        //[Range(1, int.MaxValue, ErrorMessageResourceName = "Error_HaventPutAppNameYet", ErrorMessageResourceType = typeof(Resources.Localization))]
        //[CustomValidation(typeof(MenuController), "checkApId")]
        //[Remote("CheckAppName", "Menu", ErrorMessageResourceName = "Error_HaventPutAppNameYet", ErrorMessageResourceType = typeof(Resources.Localization), HttpMethod = "POST")]
        //[Required(ErrorMessageResourceName = "Error_HaventPutAppNameYet", ErrorMessageResourceType = typeof(Resources.Localization))]
        public int AppId { get; set; }

        //[Range(1, int.MaxValue, ErrorMessageResourceName = "Error_roleID", ErrorMessageResourceType = typeof(Resources.Localization))]
        public int roleID { get; set; }

        public string AppName { get; set; }

        [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "Error_HaventPutAppNameYet", ErrorMessageResourceType = typeof(Resources.Localization))]
        //[Range(1, int.MaxValue, ErrorMessageResourceName = "Error_ParentMenuId", ErrorMessageResourceType = typeof(Resources.Localization))]
        //[Required(ErrorMessage = "Yêu cầu nhập Chức năng cha.")]
        [LocalizationDisplay("ParentMenuName")]
        public int ParentMenuId { get; set; }

        [StringLength(50, ErrorMessageResourceName = "Error_ClassNameLength", ErrorMessageResourceType = typeof(Resources.Localization))]
        [LocalizationDisplay("ClassName")]
        public string ClassName { get; set; }

        [LocalizationDisplay("MenuOrder")]
        public int MenuOrder { get; set; }
        
        public int IsActive { get; set; }
        public int RecordCount { get; set; }

        public const string FIELD_MENU_ID = "MenuId";
        public const string FIELD_MENU_NAME = "MenuName";
        public const string FIELD_MENU_CODE = "MenuCode";
        public const string FIELD_MENU_DESCRIPTION = "MenuDescription";
        public const string FIELD_APP_ID = "AppId";
        public const string FIELD_APP_NAME = "AppName";
        public const string FIELD_PARENT_MENU_ID = "ParentMenuId";
        public const string FIEL_ROLE_ID = "RoleID";
        public const string FIELD_IS_ACTIVE = "IsActive";
        public const string FIELD_RECORD_COUNT = "RecordCount";

        public MenuModel()
        {

        }

        public MenuModel(DAL.Models.Admin.MenuModel m)
        {
            // TODO: Complete member initialization
            this.MenuId = m.MenuId;
            this.MenuName = m.MenuName;
            this.MenuCode = m.MenuCode;
            this.MenuDescription = m.MenuDescription;
            this.MenuUrl = m.MenuUrl;
            this.AppId = m.AppId;
            this.AppName = m.AppName;
            this.ParentMenuId = m.ParentMenuId;
            this.roleID = m.RoleID;
            this.IsActive = m.IsActive;
            this.RecordCount = m.RecordCount;
            this.ClassName = m.ClassName;
            this.MenuOrder = m.MenuOrder;
        }

        internal DAL.Models.Admin.MenuModel toDALModel()
        {
            return new DAL.Models.Admin.MenuModel()
            {
                MenuId = this.MenuId,
                MenuName = this.MenuName,
                MenuCode = this.MenuCode,
                MenuDescription = this.MenuDescription,
                MenuUrl = this.MenuUrl,
                AppId = this.AppId,
                AppName = this.AppName,
                ParentMenuId = this.ParentMenuId,
                RoleID = this.roleID,
                IsActive = this.IsActive,
                RecordCount = this.RecordCount,
                ClassName = this.ClassName,
                MenuOrder = this.MenuOrder
            };
        }
    }
}